package com.android.hedgehog.puzzles;

import java.util.ArrayList;
import java.util.Random;

public class MathField {
	Random rand = new Random();
	ArrayList<Operand> selected = new ArrayList<Operand>();
	int[] pick = new int[3];
	int maxOperand, minOperand;
	static final int ADD = 0;
	static final int MULT = 1;
	static final int SUB = 2;
	int minTotal, maxTotal;
	int answer;
	int difficulty;
	int guess;
	Operand[] choice = new Operand[8];
	Operator operator1, operator2;
	String correctMistakeLine;

	public MathField(int difficulty) {
		this.difficulty = difficulty;
		difficultySettings(difficulty);
		enumerate();

	}

	private void difficultySettings(int difficulty) {
		minOperand = 1; // choice>=1
		maxOperand = 10; // choice<=10
		minTotal = 1; // answer>=1
		maxTotal = 999; // answer<=999

		switch (difficulty) {
		case 1:
			operator1 = new Operator(ADD);
			operator2 = new Operator(ADD);
			maxOperand = 7;
			break;
		case 2:
			operator1 = new Operator(ADD);
			operator2 = new Operator(ADD);
			minOperand = 2;
			maxOperand = 11;
			break;
		case 3:
			operator1 = new Operator(ADD);
			operator2 = new Operator(ADD);
			minTotal = 16;
			minOperand = 3;
			maxOperand = 15;
			break;
		case 4:
			operator1 = new Operator(ADD);
			operator2 = new Operator(SUB);
			minTotal = -5;
			minOperand = 1;
			maxOperand = 8;
			break;
		case 5:
			operator1 = new Operator(MULT);
			operator2 = new Operator(ADD);
			maxTotal = 55;
			maxOperand = 9;
			break;
		case 6:
			operator1 = new Operator(SUB);
			operator2 = new Operator(ADD);
			minOperand = 5;
			maxOperand = 22;
			break;
		case 7:
			operator1 = new Operator(MULT);
			operator2 = new Operator(ADD);
			minTotal = 30;
			maxTotal = 120;
			minOperand = 2;
			maxOperand = 9;
			break;
		case 8:
			operator1 = new Operator(MULT);
			operator2 = new Operator(ADD);
			minTotal = 50;
			maxTotal = 200;
			minOperand = 3;
			maxOperand = 15;

			break;
		case 9:
			operator1 = new Operator(MULT);
			operator2 = new Operator(MULT);
			minTotal = 50;
			maxTotal = 200;
			minOperand = 2;
			maxOperand = 12;

			break;
		case 10:
			operator1 = new Operator(MULT);
			operator2 = new Operator(MULT);
			minTotal = 101;
			minOperand = 5;
			maxOperand = 17;
			break;
		}

	}

	private void enumerate() {

		do {
			for (int i = 0; i < choice.length; i++) {
				choice[i] = new Operand(rand.nextInt(maxOperand - minOperand + 1) + minOperand);
			}

			pick[0] = rand.nextInt(8);
			do {
				pick[1] = rand.nextInt(8);
			} while (pick[0] == pick[1]);
			do {
				pick[2] = rand.nextInt(8);
			} while (pick[0] == pick[2] || pick[1] == pick[2]);

			int sum = 0;

			switch (operator1.value) {
			case ADD:
				sum = choice[pick[0]].value + choice[pick[1]].value;
				break;
			case MULT:
				sum = choice[pick[0]].value * choice[pick[1]].value;
				break;
			case SUB:
				sum = choice[pick[0]].value - choice[pick[1]].value;
				break;
			}
			switch (operator2.value) {
			case ADD:
				answer = sum + choice[pick[2]].value;
				break;
			case MULT:
				answer = sum * choice[pick[2]].value;
				break;
			case SUB:
				answer = sum - choice[pick[2]].value;
				break;
			}
		} while (answer < minTotal || answer > maxTotal);

		correctMistakeLine = choice[pick[0]].value + " " + operator1.chr + " " + choice[pick[1]].value + " "
				+ operator2.chr + " " + choice[pick[2]].value + " = " + answer;

	}

	public void selected(int boxNum) {
		if (selected.size() == 3)
			return;

		if (choice[boxNum].on == false) {
			choice[boxNum].on = true;
			selected.add(choice[boxNum]);
		} else {
			choice[boxNum].on = false;
			selected.remove(choice[boxNum]);
		}

	}

	public boolean checkAnswer() {

		int sum = 0;

		switch (operator1.value) {
		case ADD:
			sum = selected.get(0).value + selected.get(1).value;
			break;
		case MULT:
			sum = selected.get(0).value * selected.get(1).value;
			break;
		case SUB:
			sum = selected.get(0).value - selected.get(1).value;
			break;
		}
		switch (operator2.value) {
		case ADD:
			guess = sum + selected.get(2).value;
			break;
		case MULT:
			guess = sum * selected.get(2).value;
			break;
		case SUB:
			guess = sum - selected.get(2).value;
			break;
		}
		if (answer == guess) {
			return true;
		}

		return false;
	}

}
